var Options = chrome.extension.getBackgroundPage().Options;
(function()
{
	var options = document.getElementById("content_container").children;
	var navigation_list = document.getElementById("navigation_list");

	for(var i = 0; i < options.length; ++i)
	{
		if(options[i].id == "options_labs" && location.hash != "#labs")
			continue;
		var title = options[i].title;
		var li = document.createElement("li");
		var a = document.createElement("a");
		a.appendChild(document.createTextNode(title));
		a.href = "javascript:void(0)";
		a.title = title;
		a.onclick = (function()
		{
			var options = document.getElementById("content_container").children;
			for(var i = 0; i < options.length; ++i)
				options[i].style.display = 
					options[i].title == this.title ? "" : "none";
			document.getElementById("content_title").innerText = this.title;
		});
		if(i == 0)
			a.onclick();
		li.appendChild(a);
		navigation_list.appendChild(li);
	}

	function DumpList(div_id)
	{
		var div = document.getElementById(div_id);
		var list = [];
		for(var i = 0; i < div.children.length; ++i)
		{
			var cur = div.children[i];
			if(cur.tagName.toLowerCase() == "input" && cur.type == "text")
			{
				var text = cur.value;
				var items = text.split(/[\s,]+/);
				for(var j = 0; j < items.length; ++j)
					if(items[j])
						list[list.length] = items[j];
			}
		}
		return list;
	}

	function SaveOptions()
	{
		Options.Blocking.PostsByUsers = DumpList("options_blocking_posts");
		Options.Blocking.SignaturesOfUsers = DumpList("options_blocking_signatures");
		Options.Blocking.PhotosOfUsers = DumpList("options_blocking_photos");
		Options.Blocking.ImagesInPostsByUsers = DumpList("options_blocking_images");
	}

	function AppendInput(parent_element, sibling, value)
	{
		var input = document.createElement("input");
		input.type = "text";
		value && (input.value = value);
		input.oninput = (function()
		{
			if(!this.nextElementSibling)
				AppendInput(null, this);
		});
		input.onblur = SaveOptions;
		(parent_element || sibling.parentElement).appendChild(input);
	}

	var lists = 
	[
		{id: "options_blocking_posts", 
			value: Options.Blocking.PostsByUsers}, 
		{id: "options_blocking_signatures", 
			value: Options.Blocking.SignaturesOfUsers}, 
		{id: "options_blocking_photos", 
			value: Options.Blocking.PhotosOfUsers}, 
		{id: "options_blocking_images", 
			value: Options.Blocking.ImagesInPostsByUsers}
	];
	
	for(var i = 0; i < lists.length; ++i)
	{
		var div = document.getElementById(lists[i].id);
		for(var j = 0; j < lists[i].value.length; ++j)
			AppendInput(div, null, lists[i].value[j]);
		AppendInput(div);
	}

var stay_signedin = document.getElementById(
	"options_general_stay_signedin");
stay_signedin.onchange = (function()
{
	document.getElementById("options_general_stay_signedin_warning").style.
		display = this.checked ? "" : "none";
	Options.SignIn.StaySignedIn = this.checked;
});
stay_signedin.checked = Options.SignIn.StaySignedIn;
stay_signedin.onchange();

var signin_nowait = document.getElementById(
	"options_general_signin_nowait");
signin_nowait.onchange = (function()
{
	Options.SignIn.NoWaitAfterSignIn = this.checked;
});
signin_nowait.checked = Options.SignIn.NoWaitAfterSignIn;
signin_nowait.onchange();

var hidehotlist2 = document.getElementById(
	"options_ui_hidehotlist2");
hidehotlist2.onchange = (function()
{
	document.getElementById("options_ui_hidehotlist2_note").style.
		display = this.checked ? "" : "none";
	Options.UI.HideHotlist2 = this.checked;
});
hidehotlist2.checked = Options.UI.HideHotlist2;
hidehotlist2.onchange();

var hotlist_hideauthors = document.getElementById(
	"options_ui_hotlist_hideauthors");
hotlist_hideauthors.onchange = (function()
{
	Options.UI.HideAuthorsInHotlist = this.checked;
});
hotlist_hideauthors.checked = Options.UI.HideAuthorsInHotlist;
hotlist_hideauthors.onchange();

var simpleread = document.getElementById(
	"options_ui_simpleread");
simpleread.onchange = (function()
{
	Options.UI.SimpleRead = this.checked;
});
simpleread.checked = Options.UI.SimpleRead;
simpleread.onchange();

var DomainsInfo = chrome.extension.getBackgroundPage().DomainsInfo;
var DomainStatus = chrome.extension.getBackgroundPage().DomainStatus;
var cur_text;
if(DomainStatus["bbs.stuhome.net"] == "ok")
	cur_text = document.getElementById("internal_text");
else if(DomainStatus["bbs.stuhome.net"] == "unavailable")
	cur_text = document.getElementById("external_text");
if(cur_text)
{
	cur_text.style.fontWeight = "bold";
	cur_text.style.color = "#0000ff";
}

function updateDomain()
{
	if(Options.Domain.Preference == "default")
	{
		if(DomainStatus["bbs.stuhome.net"] == "ok")
			Options.Domain.Current = Options.Domain.PreferredInternalDomain;
		else if(DomainStatus["bbs.stuhome.net"] == "unavailable")
			Options.Domain.Current = Options.Domain.PreferredExternalDomain;
	}
	else if(Options.Domain.Preference == "alwaysExternal")
	{
		Options.Domain.Current = Options.Domain.PreferredExternalDomain;
	}
}

var always_external = document.getElementById("domain_alwaysexternal");
always_external.checked = Options.Domain.Preference == "alwaysExternal";
always_external.onchange = (function()
{
	Options.Domain.Preference = this.checked ? "alwaysExternal" : "default";
	updateDomain();
});

for(var cur in DomainsInfo)
{
	var type = DomainsInfo[cur];
	if(type == "internal" || type == "external")
	{
		var div = document.getElementById(type + "_domains");
		var input = document.createElement("input");
		input.type = "radio";
		input.name = type;
		input.value = cur;
		input.id = type + "_domain_" + cur;
		input.checked = (type == "internal" ? 
			Options.Domain.PreferredInternalDomain : 
			Options.Domain.PreferredExternalDomain) == cur;
		input.onchange = type == "internal" ? (function()
		{
			Options.Domain.PreferredInternalDomain = this.value;
			updateDomain();
		}) : (function()
		{
			Options.Domain.PreferredExternalDomain = this.value;
			updateDomain();
		});
		div.appendChild(input);
		var label = document.createElement("label");
		label.setAttribute("for", input.id);
		if(cur == Options.Domain.Current)
			label.style.fontWeight = "bold";
		label.appendChild(document.createTextNode(cur));
		div.appendChild(label);
	}
}

})();

